package com.jimet.group;

import java.sql.ResultSet;
import com.jimet.property.JimetGroupProperties;
import com.jimet.property.JimetGroupProperty;
import com.jimet.property.JimetProperty;
import com.jimet.authorization.*;
import com.jimet.user.*;

import com.jimet.db.JimetGroupDB;

public class JimetGroup {

	
	private int ID = -1;
	private String name;
	private String description;

	private JimetGroupProperties groupProps;

	
	
	public JimetGroup() {
		this.name = "";
		this.description = "";
	}
	
	public JimetAuthorizations getGroupAuthorizations() throws Exception
	{
		JimetAuthorizations groupAuths = new JimetAuthorizations();
		JimetGroupDB jimdb = new JimetGroupDB();	
		ResultSet rs = jimdb.getGroupAuthorizations(this.getID());
		while(rs.next())
		{
			JimetAuthorization jauth = new JimetAuthorization();
			jauth.setId(rs.getInt(1));
			jauth.setShortCode(rs.getString(2));
			jauth.setDescription(rs.getString(3));
			groupAuths.addAuth(jauth);
		}
		jimdb.disconnect();
		return groupAuths;
	}
	
	public void removeAllGroupAuthorizations() throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();	
		jimdb.deleteAllGroupAuthorizations(this.getID());
		jimdb.disconnect();
		
	}

	public void addAuth(String authId) throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();	
		jimdb.addGroupAuthorization(this.getID(),authId);
		jimdb.disconnect();
	}
	
	public void addAuths(String [] authIds) throws Exception{
		
		JimetGroupDB jimdb = new JimetGroupDB();	
		jimdb.addGroupAuthorizations(this.getID(),authIds);
		jimdb.disconnect();
	}
	
	public JimetGroups getSubGroups(String strSort) throws Exception
	{
		JimetGroups jgroups = new JimetGroups();
		JimetGroupDB jimdb = new JimetGroupDB();		
		ResultSet rs = jimdb.getSubGroups(this.ID, strSort);
		while(rs.next())
		{
			JimetGroup jgroup = new JimetGroup();
			jgroup.setID(rs.getInt(1));
			jgroup.setName(rs.getString(2));
			jgroup.setDescription(rs.getString(3));
			jgroups.addGroup(jgroup);
		}
		jimdb.disconnect();
		return jgroups;
	}
	
	public JimetGroups getParentGroups(String strSort) throws Exception
	{
		JimetGroups jgroups = new JimetGroups();
		JimetGroupDB jimdb = new JimetGroupDB();		
		ResultSet rs = jimdb.getParentGroups(this.ID, strSort);
		while(rs.next())
		{
			JimetGroup jgroup = new JimetGroup();
			jgroup.setID(rs.getInt(1));
			jgroup.setName(rs.getString(2));
			jgroup.setDescription(rs.getString(3));
			jgroups.addGroup(jgroup);
		}
		jimdb.disconnect();
		return jgroups;
	}
	
	public JimetUsers getUsers() throws Exception
	{
		JimetUsers jusers = new JimetUsers();
		JimetGroupDB jimdb = new JimetGroupDB();		
		ResultSet rs = jimdb.getGroupUsers(this.ID);
		while(rs.next())
		{
			JimetUser juser = new JimetUser();
			juser.load(rs.getInt(1));
			jusers.add(juser);
		}
	
		jimdb.disconnect();
		return jusers;
	}
	
	public void setParentGroups(String[] allParentsOfGroup) throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();		
			jimdb.setParentGroupsById(this.ID, allParentsOfGroup);
		jimdb.disconnect();
	}
	
	public void setSubGroups(String[] allMembersOfGroup) throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();		
			jimdb.setSubGroupsById(this.ID, allMembersOfGroup);
		jimdb.disconnect();
	}
	
	/**
	 * @return Returns the iD.
	 */
	public int getID() {
		return ID;
	}


	/**
	 * @param id The iD to set.
	 */
	public void setID(int id) {
		ID = id;
	}


	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}


	/**
	 * @return Returns the description.
	 */
	public String getDescription() {
		return description;
	}


	/**
	 * @param description The description to set.
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	public int store() throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();
		int id = -1;
		if(this.ID == -1)
			id = jimdb.addGroup(this.name,this.description);
		else
			id = jimdb.updateGroup(this.ID,this.name,this.description);
		jimdb.disconnect();
		return id;
	}

	
	public boolean delete() throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();
		boolean queryOK;
			queryOK = jimdb.deleteGroup(this.ID,this.name);
		jimdb.disconnect();
		return queryOK;
	}
	
	public static void delete(String groupID, String groupName) throws Exception
	{
			JimetGroupDB jimdb = new JimetGroupDB();
			jimdb.deleteGroup(Integer.parseInt(groupID),groupName);
			jimdb.disconnect();
	}

	public void fillGroupProps() throws Exception
	{
		this.groupProps = new JimetGroupProperties();
		
		JimetGroupDB jimdb = new JimetGroupDB();
		ResultSet rs = jimdb.getPropsByGroupId((int) this.getID());
		
		while(rs.next())
		{	
			JimetGroupProperty jproperty = new JimetGroupProperty();
			jproperty.setName(rs.getString(1));
			jproperty.setValue(rs.getString(2));
			jproperty.setId(rs.getInt(3));

			this.groupProps.add(jproperty);
		}  
		jimdb.disconnect();
	}

	public void load(String groupId) throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();
		ResultSet rs = jimdb.getGroupById(groupId);
		while(rs.next())
		{
			this.setID(rs.getInt(1));
			this.setName(rs.getString(2));
			this.setDescription(rs.getString(3));
		}
		jimdb.disconnect();
		//this.fillGroupProps();

	}
	
	public void loadByName(String groupName) throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();
		ResultSet rs = jimdb.getGroupByName(groupName);
		while(rs.next())
		{
			this.setID(rs.getInt(1));
			this.setName(rs.getString(2));
			this.setDescription(rs.getString(3));
		}
		jimdb.disconnect();
		this.fillGroupProps();

	}

	public boolean hasProperty(int propId) throws Exception
	{
		boolean hasprop = false;
		while (this.groupProps.next()) 
		{
			JimetProperty jprop = this.groupProps.getNext();
			if( jprop.getId() == propId ){
				hasprop = true;
				}
		}
		this.groupProps.reset();
		
		return hasprop;
		
	}
	
	public void removeAllUsersFromGroup() throws Exception
	{
		JimetGroupDB jimdb = new JimetGroupDB();
		jimdb.removeAllUsersFromGroup(this.getID());
		jimdb.disconnect();
		
	}

	/**
	 * @return Returns the groupProps.
	 */
	public JimetGroupProperties getGroupProps() {
		return groupProps;
	}

	/**
	 * @param groupProps The groupProps to set.
	 */
	public void setGroupProps(JimetGroupProperties groupProps) {
		this.groupProps = groupProps;
	}

	
	
	
}
